package log

import (
	"fmt"

	"os"

	. "git.oschina.net/yangdao/extlib/data_type"
)

type FileLogHandler struct {
	fileHandler *os.File
	filePath    string
}

func init() {
	AddLogHandlerCreateFunc("FileLogHandler", createFileLogHandler)
}

func createFileLogHandler(config IAssociative) ILogHandler {
	retLogHandler := &FileLogHandler{}
	if config.ContainsKey(":path") {
		retLogHandler.filePath = config.Get(":path").(string)
	}
	var err error
	retLogHandler.fileHandler, err = os.OpenFile(retLogHandler.filePath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
	if err != nil {
		panic(err)
	}
	return retLogHandler
}

func (this *FileLogHandler) Log(tags string, logValue ...interface{}) {
	str := fmt.Sprintln(logValue...)
	this.fileHandler.WriteString(str + "\r\n")
}

func (this *FileLogHandler) Close() {
	if this.fileHandler != nil {
		this.fileHandler.Close()
	}
}
